import React, { useMemo } from 'react';
import { Navigate, useRoutes } from 'react-router-dom';
import BasicLayout from '@/layout/AdminLayout';
import { getRoutes } from '@/router/fun.jsx';
import { INDEX_ROUTE } from '@/constants/pageConstant.js';

const Login = React.lazy(() => import('@/pages/Login/index.jsx'));
const NotFound = React.lazy(() => import('@/pages/NotFound/index.jsx'));
const Home = React.lazy(() => import('@/pages/Home/index.jsx'));
const UserManager = React.lazy(() => import('@/pages/UserManage/index.jsx'));
const PermissionManager = React.lazy(() => import('@/pages/PermissionManage/index.jsx'));
const RoleManager = React.lazy(() => import('@/pages/RoleManage/index.jsx'));
const FileManager = React.lazy(() => import('@/pages/FileManage/index.jsx'));
const StorageManage = React.lazy(() => import('@/pages/StorageManage/index.jsx'));
const OperationLogManage = React.lazy(() => import('@/pages/OperationLogManage/index.jsx'));

/**
 * 路由列表
 *
 * 路由对象：
 * {
 *     group: 路由分组名称，
 *     path: 路由路径，
 *     label: 路由名称，
 *     element: 路由组件，
 *     auth: 是否需要登录才能访问，
 *     isLazy: 是否懒加载，
 *     children: 子路由列表
 *     hideInMenu: 是否在菜单中隐藏
 * }
 */
const systemGroup = '系统管理';
const routeList = [
  {
    path: '/',
    label: '首页',
    element: <Navigate to={`${INDEX_ROUTE}/home`} />,
    auth: false
  },
  {
    path: `${INDEX_ROUTE}/`,
    label: '首页',
    element: <Navigate to={`${INDEX_ROUTE}/home`} />,
    auth: false
  },
  {
    path: INDEX_ROUTE,
    label: '首页',
    element: <BasicLayout />,
    isLazy: false,
    children: [
      {
        path: 'home',
        label: '仪表盘',
        element: <Home />
      },
      {
        group: systemGroup,
        path: 'user-manager',
        label: '用户管理',
        element: <UserManager />
      },
      {
        group: systemGroup,
        path: 'role-manager',
        label: '角色管理',
        element: <RoleManager />
      },
      {
        group: systemGroup,
        path: 'permission-manager',
        label: '权限管理',
        element: <PermissionManager />
      },
      {
        group: systemGroup,
        path: 'file-manager',
        label: '文件管理',
        element: <FileManager />
      },
      {
        group: systemGroup,
        path: 'storage-manage',
        label: '存储配置',
        element: <StorageManage />
      },
      {
        group: systemGroup,
        path: 'operation-log-manage',
        label: '操作日志',
        element: <OperationLogManage />
      }
    ]
  },
  {
    path: '/login',
    label: '登录',
    element: <Login />,
    auth: false
  },
  {
    path: '/404',
    label: '404',
    element: <NotFound />,
    auth: false
  },
  { path: '*', label: '404', element: <Navigate to="/404" />, auth: false }
];

// 格式化路由列表
const formatRoutes = getRoutes(routeList);

// 路由组件
const Routers = () => {
  const list = useMemo(() => formatRoutes, [routeList]);
  return useRoutes(list);
};

export { Routers, routeList };
